package com.intellagric.service.traceability.Impl.trace_company;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.TraceCompanyMapper;
import com.intellagric.module.traceability.CompanyVo;
import com.intellagric.pojo.TraceCompany;
import com.intellagric.pojo.TraceCompanyExample;
import com.intellagric.service.traceability.trace_company.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/11/6
 * @DESC： 公司信息管理服务实现
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private TraceCompanyMapper companyMapper;

    /**
     * 添加公司信息
     * @param company
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addCompany(TraceCompany company) {
        if (companyMapper.insertSelective(company) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除公司信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteCompany(String id) {
        if (companyMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改公司信息
     * @param company
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editCompany(TraceCompany company) {
        if (companyMapper.updateByPrimaryKeySelective(company) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询公司信息
     * @param id
     * @return TraceCompany
     */
    @Override
    public TraceCompany getCompanyById(String id) {
        return companyMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询公司信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCompanyList() {
        TraceCompanyExample companyExample = new TraceCompanyExample();
        companyExample.createCriteria().andIdIsNotNull();
        List<TraceCompany> companyList = companyMapper.selectByExample(companyExample);
        return LayuiDataGridResult.success().add(companyList,companyList.size());
    }

    /**
     * 分页查询公司信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCompanyPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceCompanyExample companyExample = new TraceCompanyExample();
        companyExample.createCriteria().andIdIsNotNull();
        List<TraceCompany> companyList = companyMapper.selectByExample(companyExample);
        PageInfo<TraceCompany> pageInfo = new PageInfo<>(companyList);
        return LayuiDataGridResult.success().add(companyList,pageInfo.getTotal());
    }

    /**
     * 通过Vo类型查询公司信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getCompanyByVo(int page, int rows,CompanyVo companyVo) {
        PageHelper.startPage(page,rows);
        TraceCompanyExample companyExample = new TraceCompanyExample();
        companyExample.createCriteria().andCompaneNameLike("%"+ companyVo.getCompaneName() + "%");
        List<TraceCompany> companyList = companyMapper.selectByExample(companyExample);
        PageInfo<TraceCompany> pageInfo = new PageInfo<>(companyList);
        return LayuiDataGridResult.success().add(companyList,pageInfo.getTotal());
    }
}
